package common;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.laoxinwen.app.R;

import java.util.ArrayList;
import java.util.HashMap;

import activity.ArticleActivity;
import activity.NewsActivity;
import activity.TopicActivity;
import bean.News;
import bean.TabArticleItem;
import bean.TabNewsItem;
import bean.TabTopicItem;

/**
 * 常用list item与data的糅合剂
 * Created by sshong on 15/6/25.
 */
public class CommonHolder {

    private static ArrayList<View>[] newsItemCache = new ArrayList[3];

    private static final View.OnClickListener listenerForTopic = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BaseHolder holder = (BaseHolder) v.getTag();
            BaseActivity activity = holder.getActivity();

            String tid = "";
            boolean showArticle = false;
            if (holder instanceof TopicHolder) {
                tid = holder.getTopicItem().getId();
                showArticle = false;
            } else if (holder instanceof NewsHolder) {
                tid = ((NewsHolder) holder).getNewsItem().getTid();
            } else if (holder instanceof ArticleHolder) {
                tid = ((ArticleHolder) holder).getArticleItem().getTid();
                showArticle = true;
            }
            Intent it = TopicActivity.getIntent(activity, tid, showArticle);
            activity.startActivity(it);
        }
    };
    private static final View.OnClickListener listenerForMoreArticle = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TopicHolder holder = (TopicHolder) v.getTag();
            BaseActivity activity = holder.getActivity();

            Intent it = TopicActivity.getIntent(activity, holder.getTopicItem().getId(), true);
            activity.startActivity(it);
        }
    };
    private static final View.OnClickListener listenerForNews = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NewsHolder holder = (NewsHolder) v.getTag();
            holder.clicked();
            TabNewsItem newsItem = holder.getNewsItem();
            TabTopicItem topicItem = holder.getTopicItem();
            BaseActivity activity = holder.getActivity();

            News news = new News();
            news.setId(newsItem.getId());
            news.setTid(newsItem.getTid());
            news.setTitle(newsItem.getTitle());
            if (topicItem != null) {
                news.setSummary(topicItem.getTitle());
            } else if (newsItem.getTopic() != null) {
                news.setSummary(newsItem.getTopic().getTitle());
            }
            if (null != newsItem.getImgs() && !newsItem.getImgs().isEmpty()) {
                news.setImg(newsItem.getImgs().get(0));
            }
            Intent it = NewsActivity.getIntent(activity, news);
            activity.startActivity(it);
        }
    };
    private static final View.OnClickListener listenerForArticle = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ArticleHolder holder = (ArticleHolder) v.getTag();
            holder.clicked();
            TabArticleItem articleItem = holder.getArticleItem();
            TabTopicItem topicItem = holder.getTopicItem();
            BaseActivity activity = holder.getActivity();

            News news = new News();
            news.setId(articleItem.getId());
            news.setTid(articleItem.getTid());
            news.setTitle(articleItem.getTitle());
            if (topicItem != null) {
                news.setSummary(topicItem.getTitle());
            } else if (articleItem.getTopic() != null) {
                news.setSummary(articleItem.getTopic().getTitle());
            }
            news.setImg(articleItem.getImg());
            Intent it = ArticleActivity.getIntent(activity, news);
            activity.startActivity(it);
        }
    };

    public abstract static class BaseHolder {
        protected TabTopicItem topicItem;
        protected BaseActivity activity;

        public TabTopicItem getTopicItem() {
            return topicItem;
        }

        public BaseActivity getActivity() {
            return activity;
        }
    }

    public static class TopicHolder extends BaseHolder {

        public static final int RENDER_WITH_NONE = 0;
        public static final int RENDER_WITH_SUBITEMS = 1;

        private ImageView ivTopicPic;
        private TextView tvTopicTitle;
        private TextView tvTopicSummary;
        private TextView tvCmtNum;
        private TextView tvStarNum;
        private TextView tvMoreNews;
        private TextView tvMoreArticles;
        private ViewGroup topicContainer;
        private ViewGroup newsContainer;
        private ViewGroup articleContainer;
        //data
        private int renderOption;


        public TopicHolder(View v, BaseActivity activity) {
            this(v, activity, RENDER_WITH_NONE);
        }

        public TopicHolder(View v, BaseActivity activity, int renderOption) {
            ivTopicPic = (ImageView) v.findViewById(R.id.iv_topic_pic);
            topicContainer = (ViewGroup) v.findViewById(R.id.topic_ctn);
            newsContainer = (ViewGroup) v.findViewById(R.id.news_ctn);
            articleContainer = (ViewGroup) v.findViewById(R.id.article_ctn);
            tvTopicTitle = (TextView) v.findViewById(R.id.tv_topic_title);
            tvTopicSummary = (TextView) v.findViewById(R.id.tv_topic_summary);
            tvCmtNum = (TextView) v.findViewById(R.id.tv_cmt_num);
            tvStarNum = (TextView) v.findViewById(R.id.tv_star_num);
            tvMoreNews = (TextView) v.findViewById(R.id.more_news_txt);
            tvMoreArticles = (TextView) v.findViewById(R.id.more_article_txt);

            this.activity = activity;
            this.renderOption = renderOption;

            v.setTag(this);
            v.setOnClickListener(listenerForTopic);
            v.setClickable(true);

            if(tvMoreArticles != null) {
                tvMoreArticles.setTag(this);
                tvMoreArticles.setOnClickListener(listenerForMoreArticle);
                tvMoreArticles.setClickable(true);
            }
        }

        public void setData(TabTopicItem topicItem) {
            this.topicItem = topicItem;

            if (ivTopicPic != null) {
                activity.imgPool.bind(activity.getImg(topicItem.getImg()), ivTopicPic);
            }
            if (tvTopicTitle != null) {
                tvTopicTitle.setText(topicItem.getTitle());
            }
            if (tvTopicSummary != null) {
                tvTopicSummary.setText(topicItem.getSummary());
            }
            if (tvCmtNum != null) {
                tvCmtNum.setText(topicItem.getComments_num());
            }
            if (tvStarNum != null) {
                tvStarNum.setText(topicItem.getStar_num());
            }
            recycleSubItems();
            if (renderOption == RENDER_WITH_SUBITEMS) {
                for (TabNewsItem news : topicItem.getNews()) {
                    View subItem = getSubNewsItem(news, topicItem);
                    newsContainer.addView(subItem);
                }

                for (TabArticleItem article : topicItem.getArticles()) {
                    View subItem = getSubArticleItem(article, topicItem);
                    articleContainer.addView(subItem);
                }
            }
        }

        private void recycleSubItems() {
            ViewGroup[] containers = {newsContainer, articleContainer};
            for (ViewGroup itemContainer : containers) {
                if (itemContainer != null) {
                    //缓存
                    int itemCount = itemContainer.getChildCount();
                    for (int i = 0; i < itemCount; i++) {
                        View item = itemContainer.getChildAt(i);
                        int type = (int) item.getTag(Constants.EXTRA_VIEW_DATA_TAG);
                        ArrayList<View> cacheItems = newsItemCache[type];
                        if (cacheItems == null) {
                            cacheItems = newsItemCache[type] = new ArrayList<>();
                        } else if (cacheItems.indexOf(item) != -1) {
                            continue;
                        }
                        cacheItems.add(item);
                    }
                    itemContainer.removeAllViews();
                }
            }
        }

        private View initSubItemView(int type, ViewGroup itemContainer) {
            View view;
            ArrayList<View> cacheItems = newsItemCache[type];
            if (cacheItems == null || cacheItems.isEmpty()) {
                switch (type) {
                    case 0:
                        view = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_no_pic, itemContainer, false);
                        break;
                    case 1:
                        view = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_one_pic, itemContainer, false);
                        break;
                    default:
                        view = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_three_pics, itemContainer, false);
                        break;
                }
                //记录类型，用于缓存
                view.setTag(Constants.EXTRA_VIEW_DATA_TAG, type);
            } else {
                view = cacheItems.remove(cacheItems.size() - 1);
            }
            return view;
        }

        private View getSubNewsItem(TabNewsItem tabNewsItem, TabTopicItem tabTopicItem) {
            int type = 0;
            if (null == tabNewsItem.getImgs() || tabNewsItem.getImgs().isEmpty()) {
                type = 0;
            } else if (tabNewsItem.getImgs().size() < 3) {
                type = 1;
            } else {
                type = 2;
            }
            View view = initSubItemView(type, newsContainer);
            NewsHolder holder = new NewsHolder(view, activity);
            holder.setData(tabNewsItem, tabTopicItem);
            return view;
        }

        private View getSubArticleItem(TabArticleItem tabArticleItem, TabTopicItem tabTopicItem) {
            View view = initSubItemView(1, articleContainer);
            ArticleHolder holder = new ArticleHolder(view, activity);
            holder.setData(tabArticleItem, tabTopicItem);
            return view;
        }

        public int getRenderOption() {
            return renderOption;
        }
    }

    public static class NewsHolder extends BaseHolder {
        private ImageView imgOne;
        private ImageView imgThree;
        private ImageView imgTwo;
        private TextView tvNewsMediaSrc;
        private TextView tvNewsTitle;
        private View btShowTopic;

        private TabNewsItem newsItem;

        public NewsHolder(View v, BaseActivity activity) {
            imgOne = (ImageView) v.findViewById(R.id.img_one);
            imgTwo = (ImageView) v.findViewById(R.id.img_two);//可能为null
            imgThree = (ImageView) v.findViewById(R.id.img_three);//可能为null
            tvNewsMediaSrc = (TextView) v.findViewById(R.id.tv_news_src);
            tvNewsTitle = (TextView) v.findViewById(R.id.tv_news_title);
            btShowTopic = v.findViewById(R.id.bt_show_topic);

            btShowTopic.setTag(this);
            btShowTopic.setClickable(true);
            btShowTopic.setOnClickListener(listenerForTopic);

            this.activity = activity;

            v.setTag(this);
            v.setClickable(true);
            v.setOnClickListener(listenerForNews);
        }

        public void setData(TabNewsItem newsItem) {
            this.setData(newsItem, null);
        }

        public void setData(TabNewsItem newsItem, TabTopicItem topicItem) {
            btShowTopic.setVisibility(newsItem.ableWa() && topicItem == null ? View.VISIBLE : View.GONE);

            this.newsItem = newsItem;
            this.topicItem = topicItem;

            if (null != newsItem.getImgs() && !newsItem.getImgs().isEmpty()) {
                activity.imgPool.bind(activity.getImg(newsItem.getImgs().get(0)), imgOne);
                if (newsItem.getImgs().size() > 2) {
                    activity.imgPool.bind(activity.getImg(newsItem.getImgs().get(1)), imgTwo);
                    activity.imgPool.bind(activity.getImg(newsItem.getImgs().get(2)), imgThree);
                }
            }
            tvNewsTitle.setText(newsItem.getTitle());
            tvNewsMediaSrc.setText(newsItem.getMedia() + "  " + DateUtil.autoFormat(newsItem.getTime()));
            //标题颜色
            tvNewsTitle.setTextColor(UserConfig.isReadedNews(newsItem.getId()) ? MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) : MyApp.getMyTheme().getThemeColor(R.attr.font_main_color));
        }

        public void clicked() {
            tvNewsTitle.setTextColor(MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color));
        }

        public TabNewsItem getNewsItem() {
            return newsItem;
        }
    }

    public static class ArticleHolder extends BaseHolder {
        private ImageView imgOne;
        private TextView tvNewsMediaSrc;
        private TextView tvNewsTitle;
        private View btShowTopic;

        private TabArticleItem articleItem;

        public ArticleHolder(View v, BaseActivity activity) {
            imgOne = (ImageView) v.findViewById(R.id.img_one);
            tvNewsMediaSrc = (TextView) v.findViewById(R.id.tv_news_src);
            tvNewsTitle = (TextView) v.findViewById(R.id.tv_news_title);
            btShowTopic = v.findViewById(R.id.bt_show_topic);

            btShowTopic.setTag(this);
            btShowTopic.setClickable(true);
            btShowTopic.setOnClickListener(listenerForTopic);

            this.activity = activity;

            v.setTag(this);
            v.setClickable(true);
            v.setOnClickListener(listenerForArticle);
        }

        public void setData(TabArticleItem articleItem) {
            this.setData(articleItem, null);
        }

        public void setData(TabArticleItem articleItem, TabTopicItem topicItem) {
            btShowTopic.setVisibility(articleItem.ableWa() && topicItem == null ? View.VISIBLE : View.GONE);

            this.articleItem = articleItem;
            this.topicItem = topicItem;

            activity.imgPool.bind(activity.getImg(articleItem.getImg()), imgOne);
            tvNewsTitle.setText(articleItem.getTitle());
            tvNewsMediaSrc.setText(articleItem.getMedia() + "  " + DateUtil.autoFormat(articleItem.getTime()));
            //标题颜色
            tvNewsTitle.setTextColor(UserConfig.isReadedArticle(articleItem.getId()) ? MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) : MyApp.getMyTheme().getThemeColor(R.attr.font_main_color));
        }

        public void clicked() {
            tvNewsTitle.setTextColor(MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color));
        }

        public TabArticleItem getArticleItem() {
            return articleItem;
        }
    }
}
